package com.zzxx.service.impl;

import com.zzxx.entity.*;
import com.zzxx.mapper.*;
import com.zzxx.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private UserCouponMapper userCouponMapper;
    @Autowired(required = false)
    private HttpServletRequest request;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private GymMapper gymMapper;
    @Override
    public Coupon find(String id) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria().andIdEqualTo(id);
        List<Coupon> coupons = couponMapper.selectByExample(example);
        return coupons.get(0);
    }


    @Override
    public List<Coupon> findList(String userId) {
        UserCouponExample ucexample = new UserCouponExample();
        ucexample.createCriteria().andUseridEqualTo(userId);
        List<UserCoupon> userCoupons = userCouponMapper.selectByExample(ucexample);
        CouponExample example = new CouponExample();
        userCoupons.forEach(n->{
            example.or().andIdEqualTo(n.getCouponid());
        });
        List<Coupon> coupons = couponMapper.selectByExample(example);
        return coupons;
    }


    @Override
    public void findUserCouponInfo(UserCouponInfo info) {
        String uid = info.getUser().getId();

        //找到UserCoupons
        UserCouponExample ucexample = new UserCouponExample();
        ucexample.createCriteria().andUseridEqualTo(uid);
        List<UserCoupon> userCoupons = userCouponMapper.selectByExample(ucexample);
        //找到Coupons
        for (int i = 0; i < userCoupons.size(); i++) {
            UserCoupon userCoupon = userCoupons.get(i);
            Coupon coupon = couponMapper.selectByPrimaryKey(userCoupon.getCouponid());


            //找不到coupon，代表优惠券已被删除；
            //如果usercoupon状态为1，代表已被使用;
            //如果usercoupon状态为-1，代表已过期;
            //coupon过期
            // 如果以上情况进行删除
            if (coupon==null||userCoupon.getStatus()==1||userCoupon.getStatus()==-1){
                userCouponMapper.deleteByPrimaryKey(userCoupon.getId());
                userCoupons.remove(i);
                i--;
                continue;
            }
            Integer indate = coupon.getIndate();
            Date activetime = userCoupon.getActivetime();
            Calendar _lastTime = Calendar.getInstance();
            _lastTime.setTime(activetime);
            _lastTime.add(Calendar.DAY_OF_YEAR,indate);
            Date lastTime = _lastTime.getTime();
            //如果过期，将状态置为-2，下次删除
            if (lastTime.before(new Date())){
                userCoupon.setStatus(-1);
                userCouponMapper.updateByPrimaryKey(userCoupon);
                continue;
            }
            //完善当前coupon的name
            String parent = "";
            String targetid = coupon.getTargetid();
            Ticket ticket = ticketMapper.selectByPrimaryKey(targetid);
            if (ticket==null){
                Activity activity = activityMapper.selectByPrimaryKey(targetid);
                parent = activity.getActivityname();
            }else{
                Gym gym = gymMapper.selectByPrimaryKey(ticket.getTargetid());
                parent = gym.getName()+ticket.getName();
            }
            coupon.setName(parent+coupon.getName());
            //添加coupon和时间
            info.getCoupons().add(coupon);
            info.getUserCoupons().add(userCoupon);
            info.getLastTimes().add(lastTime);
        }
    }

    @Override
    public List<Coupon> findAll(String uid) {
        List<Coupon> coupons = couponMapper.findByNoUid(uid);
        return coupons;
    }

    @Override
    public void addCoupon(String id) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria().andIdEqualTo(id);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        Coupon coupon = new Coupon();
        coupon=coupons.get(0);
        coupon.setLastnum(coupon.getLastnum()-1);
        couponMapper.updateByPrimaryKeySelective(coupon);
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        String uuid = UUID.randomUUID().toString().replace("-", "");
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setId(uuid);
        userCoupon.setUserid(loginUser.getId());
        userCoupon.setCouponid(id);
        userCoupon.setStatus(0);
        userCoupon.setActivetime(new Date());
        userCouponMapper.insertSelective(userCoupon);
    }
}
